package com.sywb.chuangyebao.contract;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.bean.UserInfo;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.BindMobileSuccessActivity;

import org.bining.footstone.db.DbManager;
import org.bining.footstone.utils.SharedUtils;

/**
 * 开始界面的Contract
 */
public interface BindContract {

    /**
     * 开始界面要实现的View
     */
    interface View extends BaseTokenContract.View {
    }

    /**
     * 开始界面要绑定的Presenter
     */
    class Presenter extends BaseTokenContract.Presenter<View> {

        @Override
        public void onStart() {
            super.onStart();
        }

        public void bindMobile(final String mobile, String code) {
            SharedUtils.put(Constants.USERACCOUNT, mobile);
            SharedUtils.put(Constants.USERCAPTCHA, code);

            HttpUtils.bind(new HttpCallback<Object>() {

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onSuccess(Object o) {
                    // showMessage("绑定成功!");
                    // 更新本地数据库用户手机号码
                    String openId = SharedUtils.getString(Constants.USEROPENID);
                    UserInfo userInfo = DbManager.getInstance().queryById(openId, UserInfo.class);
                    userInfo.mobile = mobile;
                    userInfo.mobile_verify = 1;
                    DbManager.getInstance().update(userInfo);

                    SharedUtils.put(Constants.USERMOBILEVERIFY, true);

                    mView.advance(BindMobileSuccessActivity.class, mobile);
                    mView.exit();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }
    }

}

